package com.fangrui.controller;

import com.fangrui.pojo.Result;
import com.fangrui.pojo.User;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;

//
// Created by ✎﹏ℳ๓独赏☆独醉 on 2025/4/4 12:54.
//
@RestController
public class HelloController {
    @RequestMapping("/hello")
    public Result hello() {
        return Result.success("Hello World") ;
    }
    @RequestMapping("simpleParamOld")
    public Result simpleParam(HttpServletRequest request) {
        String name = request.getParameter("name");
        int age = Integer.parseInt(request.getParameter("age"));
        return Result.success("Hello " + name + ", " + age + "!");
    }
    @RequestMapping("simpleParamNew")
    public Result simpleParam(String name, Integer age) {
        return Result.success("Hello " + name + ", " + age + "!");
    }
    @RequestMapping("postParamOld")
    public Result postParamOld(String username,int age) {
        return Result.success("Hello " + username + ", " + age + "!");
    }
    @RequestMapping("postParamNew")
    public Result postParamNew(@RequestParam(name = "name",required = false) String username,int age) {
        //一旦添加上@RequestParam(name = "name")，则这个参数必须传递，因为其具有一个require属性，默认值为true，可以在后面申明为false取消这一特性
        return Result.success("Hello " + username + ", " + age + "!");
    }

    @RequestMapping("postSimpleParam")
    public Result postSimpleParam(User user) {
        //可自动将请求中的参数封装为实体，但需要请求中的参数名与实体属性名一致
        return Result.success(user);
    }

    @RequestMapping("postComplexParam")
    public Result postComplexParam(User user) {
        return Result.success(user);
    }

    @RequestMapping("arrayParam")
    public Result arrayParam(@RequestParam(name="hobby") String[] hobbies) {
        return Result.success(hobbies);
    }

    @RequestMapping("listParam")
    public Result listParam(@RequestParam(name="hobby") List<String> hobbies) {
        return Result.success(hobbies);
    }

    @RequestMapping("dateParam")
    public Result dateParam(@DateTimeFormat(pattern =  "yyyy-MM-dd HH:mm:ss") LocalDateTime localDateTime) {
        return Result.success(localDateTime);
    }

    @RequestMapping("jsonParam")
    public Result jsonParam(@RequestBody User user) {
        return Result.success(user);
    }

    @RequestMapping("pathParam/{id}")
    public Result pathParam(@PathVariable int id) {
        return Result.success(id);
    }

    @RequestMapping("pathParam/{id}/{name}")
    public Result pathParam(@PathVariable int id, @PathVariable String name) {
        return Result.success(id + ":" + name);
    }
}
